﻿<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BoxChungKhoan.ascx.cs" Inherits="GUI.Controls.Shared.BoxChungKhoan" %>
<div class="w180 fl box-gia-vang">
    <p>
        <span class="icoDollar sprite"></span><span id="dollarContent"></span>
    </p>
    <p>
        <span class="icoGold sprite"></span><span id="goldContent"></span>
    </p>
</div>
<table class="box-chung-khoan">

</table>
<script type="text/javascript">
    var indexCallBack;
    var boxTiGia = function ($) {

        var boxTiGiaInterval = undefined;

        function getData() {
            indexCallBack = function (response) {
                displayData(response);
                if (typeof boxTiGiaInterval == "undefined") {
                    $(".box-gia-vang, .box-chung-khoan").show();
                    boxTiGiaInterval = setInterval(getData, 15000);
                }
            };

            $.getScript('http://solieu7.vcmedia.vn/IndexHandler.ashx');

        }

        function onError() {
            $(".box-gia-vang, .box-chung-khoan").hide();
        }

        function displayData(json) {
            var html = '';
            json = $.parseJSON(json);
            for (var i = 0; i < json.length; i++) {
                var item = json[i];
                switch (item.Name) {
                    case "Vàng SJC":
                        {
                            $("#goldContent").text(String.format("{0}đ/lượng", item.Value.replace('.00', ',000.00')));
                            break;
                        }
                    case "USD (VCB)":
                        {
                            $("#dollarContent").text(String.format("1USD = {0}đ", item.Value));
                            break;
                        }
                    default:
                        if (item.Name == 'DowJones' ||
                    item.Name == 'Crude Oil' ||
                    item.Name == 'Gold') {
                            var changePercent = parseFloat(item.ChangePercent);
                            var change = changePercent > 0
                        ? String.format('<span class="increase">▲ {0}</span>', changePercent)
                        : String.format('<span class="decrease">▼ {0}</span>', changePercent * -1);
                            html += String.format('<tr><th>{0}</th><td>{1}</td><td>{2}</td><td>{3}</td></tr>', item.Name, item.Value, ((item.Change.toString().indexOf('-') == -1) && (item.Change.toString().indexOf('+') == -1)) ? '+' + item.Change : item.Change, change);
                        }
                        break;
                }
                $('.box-chung-khoan').html(html);
            }
        }

        return {
            init: function () {
                onError();
                getData();
            }
        };
    } (jQuery);

    boxTiGia.init();
</script>
